import './add.less'
import { TreeToArray } from '@/utils'

import dictsMixin from '@/mixins/dicts'
import uploadMixin from '@/mixins/upload'

import { formatDate } from '@/mixins/filters'

import layer from '@/components/layer'

export default {
    mixins: [dictsMixin, uploadMixin],
    data() {
        return {
            id: this.$route.query.id || 0,
            clueDetail: {
                clueInformer: {},
                clueBeReported: [{}],
                clueInfo: {},
                customer: [{}]
            },
            search: {
                loading: false,
                list: [],
                timer: null
            },
            // 分部负责人列表
            deptPrincipalList: [],
            // 分部负责人
            deptPrincipal: {
                name: ''
            },
            //所属营业部
            salesDeptData: {
                loading: false,
                timer: null,
                list: []
            },
            // 客户信息个数
            customerCount: 1,
            // 举证资料
            evidenceData: {
                layer: {
                    show: false,
                    title: '上传举证资料'
                },
                form: {},
                list: []
            }
        }
    },
    computed: {
    },
    components: {
        layer
    },
    mounted() {
        this.$store.dispatch('setBreadData', [
            {
                name: '线索管理'
            },
            {
                name: '添加线索'
            }
        ])

        this.uploadData.limit = 1

        if (this.id) {
            this.getClueDetail()
        }
    },
    methods: {
        // 保存
        submitForm() {
            if (this.clueDetail.clueInformer.reflectPhone) {
                if (!/^1[3|5|6|7|8|9]\d{9}$/.test(this.clueDetail.clueInformer.reflectPhone)) {
                    this.$msg.error('举报人手机号格式不正确')
                    return false
                }
            }
            if (this.clueDetail.clueBeReported.beReportedPhone) {
                if (!/^1[3|5|6|7|8|9]\d{9}$/.test(this.clueDetail.clueBeReported.beReportedPhone)) {
                    this.$msg.error('被举报人手机号格式不正确')
                    return false
                }
            }
            if (!this.clueDetail.clueInfo.source) {
                this.$msg.error('请选择来源渠道')
                return false
            }
            if (!this.clueDetail.clueInfo.product) {
                this.$msg.error('请选择产品类型')
                return false
            }
            if (!this.clueDetail.clueInfo.reflectContent) {
                this.$msg.error('请输入反馈详情')
                return false
            }

            if (this.deptPrincipal.name) {
                this.clueDetail.clueInfo.salesName = this.deptPrincipal.name
            }

            if (this.fileList.length) {
                this.clueDetail.clueEvidences = []
                this.fileList.forEach(item => {
                    let _obj = {};
                    _obj.fileName = item.name
                    _obj.fileUrl = item.url
                    this.clueDetail.clueEvidences.push(_obj)
                });
            }

            this.clueDetail.clueInformer.creditMoney = this.clueDetail.clueInformer.creditMoney ? parseInt(this.clueDetail.clueInformer.creditMoney).toFixed(2) : ''
            this.clueDetail.clueInformer.borrowMoney = this.clueDetail.clueInformer.borrowMoney ? parseInt(this.clueDetail.clueInformer.borrowMoney).toFixed(2) : ''

            if (this.riskCategoryForm.id) {
                let _category = this.dicts.riskCategory.find(item => item.id == this.riskCategoryForm.id)
                if (_category) {
                    this.clueDetail.clueInfo.riskCategory = _category.value
                }
            }
            if (this.riskTypeForm.value) {
                this.clueDetail.clueInfo.riskType = this.riskTypeForm.value
            }
            if (this.riskLevelForm.value) {
                this.clueDetail.clueInfo.riskLevel = this.riskLevelForm.value
            }
            api.addClue(this.clueDetail).then(res => {
                this.$msg.success('添加线索成功')
                this.$store.dispatch('getUnProcess')
                this.$router.replace('/clue/list')
            }).catch(res => {
                console.log(res)
            })
        },
        // 返回
        goBack() {
            this.$router.go(-1)
        },
        // 线索详情
        getClueDetail() {
            api.queryClueInfoById({ id: this.id }).then(res => {
                if (res.clueInformer.reflectSex != undefined) {
                    res.clueInformer.reflectSexName = res.clueInformer.reflectSex == 1 ? '男' : '女'
                }
                if (res.clueInformer.managerChange != undefined) {
                    res.clueInformer.managerChangeName = res.clueInformer.managerChange == 1 ? '是' : '否'
                }
                res.clueInformer.creditDate = res.clueInformer.creditDate ? this.filter.formatDate(res.clueInformer.creditDate, 'yyyy-MM-dd') : ''
                res.clueInformer.borrowDate = res.clueInformer.borrowDate ? this.filter.formatDate(res.clueInformer.borrowDate, 'yyyy-MM-dd') : ''
                res.clueInfo.clueDate = res.clueInfo.clueDate ? this.filter.formatDate(res.clueInfo.clueDate, 'yyyy-MM-dd') : ''

                this.deptPrincipal.name = res.clueInfo.salesName

                if (res.clueInfo.source) {
                    let _obj = this.dicts.sourceType.find(o => o.value == res.clueInfo.source)
                    if (_obj) {
                        res.clueInfo.sourceName = _obj.name
                    }
                }
                if (res.clueInfo.product) {
                    let _obj = this.dicts.productType.find(o => o.value == res.clueInfo.product)
                    if (_obj) {
                        res.clueInfo.productName = _obj.name
                    }
                }

                if (res.clueInfo.riskCategory) {
                    let _category = this.dicts.riskCategory.find(item => item.value == res.clueInfo.riskCategory)
                    if (_category) {
                        this.riskCategoryForm.id = _category.id
                        if (res.clueInfo.riskType) {
                            this.riskCategoryChange(_category.id)
                            this.riskTypeForm.value = res.clueInfo.riskType
                        }
                        if (res.clueInfo.riskLevel) {
                            this.riskTypeChange(res.clueInfo.riskType)
                            this.riskLevelForm.value = res.clueInfo.riskLevel
                        }
                    }
                }

                this.clueDetail = res
                this.caseStatus = this.clueDetail.clueInfo.clueStatus !== 0 && this.clueDetail.clueInfo.clueStatus !== 3

                this.clueDetail.customer = [{}]
                this.clueDetail.clueBeReported = [{}]


                // 举报人所属营业部
                if (this.clueDetail.clueInformer.salesDeptCode) {
                    let _obj = {}
                    _obj.id = this.clueDetail.clueInformer.salesDeptCode
                    _obj.deptName = this.clueDetail.clueInformer.salesDeptName
                    this.salesDeptData.list = []
                    this.salesDeptData.list.push(_obj)
                }

                this.getProposalData(1)

            }).catch(res => {
                console.log(res)
            })
        },
        // 搜索被举报人所属公司和被举报人所属部门
        searchBusinessMethod(val) {
            if (this.salesDeptData.timer) {
                clearTimeout(this.salesDeptData.timer)
            }
            if (val !== '') {
                this.search.loading = true;
                this.search.timer = setTimeout(() => {
                    api.queryDepartment({
                        name: val
                    }).then(res => {
                        this.search.loading = false
                        this.search.list = res.department
                    }).catch(res => {
                        console.log(res)
                    })
                }, 200)
            }
            else {
                this.search.list = []
            }
        },
        // 选择所属分部
        selectSalesDeptName(name) {
            this.deptPrincipal = {}
            let id = this.deptList.find(res => res.name === name).id
            api.queryUser({
                deptId: id,
                pageSize: 100,
                pageIndex: 1
            }).then(res => {
                this.deptPrincipalList = res.list
            }).catch(res => {
                console.log(res)
            })
        },
        // 请搜索所属营业部
        searchSalesDept(val) {
            if (this.salesDeptData.timer) {
                clearTimeout(this.salesDeptData.timer)
            }
            if (val !== '') {
                this.salesDeptData.loading = true
                this.salesDeptData.timer = setTimeout(() => {
                    let _data = {}
                    _data.deptName = val
                    _data.pageBean = {}
                    _data.pageBean.pageIndex = 1
                    _data.pageBean.pageSize = 1000
                    api.queryDeptJc(_data)
                        .then(res => {
                            this.salesDeptData.loading = false
                            this.salesDeptData.list = res.list
                        })
                        .catch(err => {
                            this.salesDeptData.loading = false
                            console.log(err)
                        })
                }, 200)
            } else {
                this.salesDeptData.list = []
            }
        },
        // 选择所属营业部
        salesDeptChange(val) {
            if (val) {
                let _obj = this.salesDeptData.list.find(d => d.id == val)
                if (_obj) {
                    this.clueDetail.clueInformer.salesDeptName = _obj.deptName
                }
            }
        },

        // 新增客户信息
        addCustomer() {
            this.clueDetail.customer.push({})
            this.customerCount++
        },
        // 移除客户信息
        removeCustomer(i) {
            this.customerCount--
            this.clueDetail.customer.splice(i, 1)
        },

        // 新增被举报人信息
        addBeReported() {
            this.clueDetail.clueBeReported.push({})
        },
        // 移除被举报人信息
        deleteBeReported(i) {
            this.clueDetail.clueBeReported.splice(i, 1)
        },
        // 显示举证资料弹窗
        showEvidenceLayer() {
            this.evidenceData.layer.show = true
            this.evidenceData.form = {}
            this.uploadData.fileList = []
        },
        // 保存举证资料
        saveEvidence() {
            if (!this.evidenceData.form.fileName) {
                this.$msg.error('请输入文件名称')
                return
            }
            // if (this.uploadData.fileList.length == 0) {
            //     this.$msg.error('请选择上传文件')
            //     return
            // }

            let _obj = {}
            if (this.uploadData.fileList.length) {
                let _file = this.uploadData.fileList[0]
                _obj.fileUrl = _file.url
            }
            _obj.fileName = this.evidenceData.form.fileName
            _obj.remark = this.evidenceData.form.remark
            _obj.cdate = formatDate(Date.now())
            this.evidenceData.list.push(_obj)

            this.$msg.success('添加成功')
            this.evidenceData.layer.show = false
        },
        deleteEvidence(i) {
            this.evidenceData.list.splice(i, 1)
            console.log(this.evidenceData.list)
        }
    }
}